package com.nefu.softlab.xjwc.controller;

import com.nefu.softlab.xjwc.service.ExportExcelService;
import com.nefu.softlab.xjwc.utils.JSONResult;
import com.nefu.softlab.xjwc.utils.XJWCException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author LiXiwen
 * @date 2020/4/15 15:05
 */
@RestController
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
@Slf4j
public class ExportExcelApi {

    private final ExportExcelService exportExcelService;

    @Autowired
    public ExportExcelApi(ExportExcelService exportExcelService) {
        this.exportExcelService = exportExcelService;
    }

//    @GetMapping("/excel/project/export/{projectId}")
//    public JSONResult exportProjectExcel(@PathVariable("projectId") Integer projectId) {
//        if (StringUtils.isEmpty(projectId)) {
//            return JSONResult.errorMessage("项目id不能为空!");
//        }
//        return null;
//        //return JSONResult.ok(exportExcelService.exportExcelByProjectId(projectId));
//    }
//
//    @GetMapping("/excel/project/download/{projectId}")
//    public JSONResult getProjectExcel(@PathVariable("projectId") Integer projectId) {
//        return null;
//    }


//    @GetMapping("/excel/project-total/export/{projectId}/{excelName}")
//    public JSONResult exportTotalProjectExcel(@PathVariable("projectId") Integer projectId, @PathVariable("excelName") String excelName) {
//        if (StringUtils.isEmpty(projectId)) {
//            return JSONResult.errorMessage("项目id不能为空!");
//        }
//        if (StringUtils.isEmpty(excelName)) {
//            return JSONResult.errorMessage("工作表名不能为空!");
//        }
//        return JSONResult.ok(exportExcelService.exportTotalProjectExcelByProjectId(projectId));
//    }

    @GetMapping("/excel/project-total/export/{projectId}")
    public String exportTotalProjectExcel(@PathVariable("projectId") Integer projectId, HttpServletResponse response, HttpServletRequest request) {
        if (StringUtils.isEmpty(projectId)) {
            return "项目id不能为空!";
        }

        return exportExcelService.exportTotalProjectExcelByProjectId(projectId, response, request);
    }

    @GetMapping("/excel/activity/export/{activityId}")
    public String exportExcelByActivityId(@PathVariable("activityId") Integer activityId, HttpServletResponse response, HttpServletRequest request) throws XJWCException {
        if (StringUtils.isEmpty(activityId)) {
            return "活动id不能为空!";
        }
        log.info("导出活动下项目汇总表 , activityId : " + activityId);

        return exportExcelService.exportExcelByActivityId(activityId, response, request);
    }

    // http://127.0.0.1:8080/excel/project/export/1/5a845d3a52264b2b8bde8d3fae10da72
    @GetMapping("/excel/project/export/{projectId}/{userId}")
    public String exportExcelByUserIdAndProId(@PathVariable("projectId") Integer projectId, @PathVariable("userId") String userId, HttpServletResponse response, HttpServletRequest request) {
        if (StringUtils.isEmpty(projectId)) {
            return "项目id不能为空!";
        }
        if (StringUtils.isEmpty(userId)) {
            return "用户id不能为空!";
        }
        return exportExcelService.exportExcelByUserIdAndProId(projectId, userId, response, request);
    }


    @GetMapping("/excel/preview/{projectId}")
    public String priviewTotalExcel(@PathVariable("projectId") Integer projectId, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(projectId)) {
            return "项目id不能为空";
        }
        log.info("GET priviewTotalExcel : projectId : " + projectId);

        return exportExcelService.exportTotalProjectExcelByProjectId(projectId, response, request);
    }


    @GetMapping("/excel/project-total/download/{projectId}")
    public JSONResult getTotalProjectExcel(@PathVariable("projectId") Integer projectId, HttpServletResponse response, HttpServletRequest request) throws XJWCException {
        if (StringUtils.isEmpty(projectId)) {
            return JSONResult.errorMessage("项目id不能为空!");
        }

        return JSONResult.ok(exportExcelService.downloadTotalProjectExcelByProjectId(projectId, response, request));
    }

    @PostMapping("/excel/upload")
    public JSONResult uploadExcelModel(@RequestPart(value = "file") MultipartFile[] files) {
        if (files == null || files.length <= 0) {
            return JSONResult.errorMessage("文件不能为空, 请重新上传!");
        }
        return JSONResult.ok(exportExcelService.uploadExcelModel(files));
    }

    @GetMapping("/excel/get")
    public JSONResult getExcelModels() {
        return JSONResult.ok(exportExcelService.getExcelModels());
    }

}